package com.homwee.listMenu.util;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;



public class MenuItemData{
	public interface onEnterRightKey{
		void onEnterRightKey();
	}
	public enum EN_ItemType{
		EN_ROOTVIEW,
		EN_OPTIONVIEW,
		EN_OPTIONINPUTVIEW,
		EN_PROGRESSBAR, 
		EN_HAVESUBCHILD,
		EN_OPTIONTEXTVIEW,
	}
	
	public interface onValueChange{
		public void valueChange(int value);
	}
	 public String mId =null;
	 private int mhelpId;
	 public int mNameImage;
	 public String mName; 
	 
     public int mInitValue; 
     public String[] mOptionValue;
     public String mOptionStr;
     public float mStepValue; 
 
     public int mMinValue; 
     public int mMaxValue;
     public boolean isCycle =true;
     public  EN_ItemType mItemType;
     public MenuItemData mParent;
     public List<MenuItemData> mParentGroup;
     public List<MenuItemData> mSubChildGroup;
     public String mTitleName =null;
	 public Integer mTotalPages;
     public MenuItemView view =null;
     public boolean isEnable =true;
     private List<onValueChange> onChangeFunction =null;
     public int mShowMySelf =0;
     private onEnterRightKey mOnEnterRightKey;
     private onEnterBackKey mOnEnterBackKey;
     
     /**zcj add for show different color*/
     public int mSpecialColor=0;
     
     public boolean mPassword =true;
     
     public MenuItemData(int mNameImage ,String mName , int mMinValue, int mMaxValue, int mInitValue,
             String[] mOptionValue, float mStepValue, EN_ItemType itemType ,int mShowMySelf){
		 this.mNameImage =mNameImage;
		 this.mName =mName;
		 this.mMinValue =mMinValue;
		 this.mMaxValue =mMaxValue;
		 this.mInitValue =mInitValue;
		 this.mOptionValue =mOptionValue;
		 this.mStepValue =mStepValue;
		 this.mItemType =itemType;
		 this.mShowMySelf =mShowMySelf;
		 if(itemType ==EN_ItemType.EN_OPTIONVIEW && mMinValue ==mMaxValue){
			 isEnable =false;
		 }
	 }
     public MenuItemData(String mName){
    	 this.mName =mName;
		 this.mItemType =EN_ItemType.EN_HAVESUBCHILD;
	 }
     public MenuItemData(int mNameImage ,String mName , int mMinValue, int mMaxValue, int mInitValue,
             String[] mOptionValue, float mStepValue, EN_ItemType itemType ){
		 this.mNameImage =mNameImage;
		 this.mName =mName;
		 this.mMinValue =mMinValue;
		 this.mMaxValue =mMaxValue;
		 this.mInitValue =mInitValue;
		 this.mOptionValue =mOptionValue;
		 this.mStepValue =mStepValue;
		 this.mItemType =itemType;
		 if(itemType ==EN_ItemType.EN_OPTIONVIEW && mMinValue ==mMaxValue){
			 isEnable =false;
		 }
	 }
     public MenuItemData(int mNameImage ,String mName , int mMinValue, int mMaxValue, int mInitValue,
             float mStepValue, EN_ItemType itemType ){
		 this.mNameImage =mNameImage;
		 this.mName =mName;
		 this.mMinValue =mMinValue;
		 this.mMaxValue =mMaxValue;
		 this.mInitValue =mInitValue;
		 this.mStepValue =mStepValue;
		 this.mItemType =itemType;
	 }     
     public MenuItemData(int mNameImage ,String mName , int mMinValue, int mMaxValue, int mInitValue,
             String mOptionValue, float mStepValue){
		 this.mNameImage =mNameImage;
		 this.mName =mName;
		 this.mMinValue =mMinValue;
		 this.mMaxValue =mMaxValue;
		 this.mInitValue =mInitValue;
		 this.mOptionStr =mOptionValue;
		 this.mStepValue =mStepValue;
		 this.mItemType =EN_ItemType.EN_OPTIONTEXTVIEW;
//		 if(mMinValue ==mMaxValue){
//			 isEnable =false;
//		 }
	 }     
     public MenuItemData(String mId ,int mNameImage ,String mName , int mMinValue, int mMaxValue, int mInitValue,
             String[] mOptionValue, float mStepValue, EN_ItemType itemType ){
    	 this.mId =mId;
		 this.mNameImage =mNameImage;
		 this.mName =mName;
		 this.mMinValue =mMinValue;
		 this.mMaxValue =mMaxValue;
		 this.mInitValue =mInitValue;
		 this.mOptionValue =mOptionValue;
		 this.mStepValue =mStepValue;
		 this.mItemType =itemType;
		 if(itemType ==EN_ItemType.EN_OPTIONVIEW && mMinValue ==mMaxValue){
			 isEnable =false;
		 }
		 
	 }
     
	public MenuItemData(String mId, String mName, String valueStrings,
			EN_ItemType itemType) {
		this.mId = mId;
		this.mName = mName;
		this.mItemType = itemType;
		this.mOptionStr = valueStrings;
	}
     
	 public void setOnValueChange(onValueChange onChange){
		 if(onChangeFunction ==null){
			 onChangeFunction =new ArrayList<onValueChange>();
		 }
		 if(onChangeFunction.size()<3){
		 onChangeFunction.add(onChange);
		 }
	 }
	 public boolean onLeftKey(){
		 Log.v("tv","onLeftKey");
		if (mItemType == EN_ItemType.EN_OPTIONVIEW) {
			if (mInitValue == mMinValue) {
				if(isCycle){
					mInitValue = mMaxValue;
				}else{
					mInitValue = mMinValue;
				}
			} else {
				mInitValue--;
			}
			valueChange();
			return true;
		} else if(mItemType == EN_ItemType.EN_PROGRESSBAR){
			if(mInitValue>mMinValue){
				mInitValue =(int) (mInitValue -mStepValue);
				valueChange();
			}
			return true;
		} else if(mItemType == EN_ItemType.EN_OPTIONTEXTVIEW){
			mInitValue -=mStepValue;
			if(mInitValue <mMinValue){
				if(isCycle){
					mInitValue = mMaxValue;
				}else{
					mInitValue = mMinValue;
				}
			}
			valueChange();
			return true;
		}else{
			return false;
		}
	 }
	 public boolean onRightKey(){
		 Log.v("tv","onRightKey");
		if (mItemType == EN_ItemType.EN_OPTIONVIEW) {
			if (mInitValue == mMaxValue) {
				if(isCycle){
					mInitValue = mMinValue;
				}else{
					mInitValue = mMaxValue;
				}
			} else {
				mInitValue++;
			}
			valueChange();
			return true;
		} else if(mItemType == EN_ItemType.EN_PROGRESSBAR){
			if(mInitValue <mMaxValue){
				mInitValue =(int) (mInitValue +mStepValue);
				valueChange();
			}
			return true;
		}else if(mItemType == EN_ItemType.EN_HAVESUBCHILD){
			return onEnterKey();
		}else if(mItemType == EN_ItemType.EN_OPTIONTEXTVIEW){
			mInitValue +=mStepValue;
			if(mInitValue >mMaxValue){
				if(isCycle){
					mInitValue = mMinValue;
				}else{
					mInitValue = mMaxValue;
				}
				
			}
			valueChange();
			return true;
		}else {
			return false;
		}
	 }

	public boolean onEnterKey(){
		if(mOnEnterRightKey !=null){
			mOnEnterRightKey.onEnterRightKey();
			return true;
		}else{
			return false;
		}
	}  
	public void valueChange() {
		Log.v("tv","valueChange");
		if (onChangeFunction != null) {
			for (onValueChange function : onChangeFunction) {
				Log.v("tv","function.valueChange(mInitValue)");
				function.valueChange(mInitValue);
			}
		}
	}
	public void setView(MenuItemView view){
		this.view =view;
	}
	public MenuItemView getView(){
		return view;
	}
	public void update(){
		if(view !=null)
		view.update();
	}
	public void updateValue(int value){
		mInitValue =value;
		update();
	}
	public void updateValue(String value){
		mOptionStr =value;
		update();
	}
	public void setEnable(boolean value){
		isEnable =value;
		update();
	}
	public void setOnEnterKey(onEnterRightKey onKey){
		mOnEnterRightKey =onKey;
	}
	public void setOnBackKey(onEnterBackKey onKey){
		mOnEnterBackKey =onKey;
	}
	
	public interface onEnterBackKey{
		void onEnterBackKey();
	}
	
	public boolean onEnterBackKey(){
		if(mOnEnterBackKey !=null){
			mOnEnterBackKey.onEnterBackKey();
			return true;
		}else{
			return false;
		}
	}  
	/**zcj add for show different color*/
	public void setSpecialColor(int color) {
		this.mSpecialColor=color;
	}
	public void UpdateSpecialColor(int color) {
		this.mSpecialColor=color;
		update();
	}
}
